package org.dtrd.modules.content.articles.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Hash;
import org.dtrd.common.util.StringUtil;
import org.dtrd.modules.content.articles.entity.bean.ArticleCategory;
import org.dtrd.modules.content.articles.entity.po.DtrdExtEmsCategory;
import org.dtrd.modules.content.articles.entity.request.EditArticleCategoryRequest;
import org.dtrd.modules.content.articles.mapper.DtrdExtEmsCategoryMapper;
import org.dtrd.modules.content.articles.service.IDtrdExtEmsCategoryService;
import org.dtrd.modules.content.articles.service.IDtrdExtEmsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 萧
 * @date 2021-08-25 17:37:46
 **/
@Service
@DS("multi-datasource1")
@Slf4j
public class DtrdExtEmsCategoryServiceImpl extends ServiceImpl<DtrdExtEmsCategoryMapper, DtrdExtEmsCategory> implements IDtrdExtEmsCategoryService {

    @Autowired
    private IDtrdExtEmsContentService contentService;

    @Override
    public List<ArticleCategory> selectCategories(Integer parentId) {
        LambdaQueryWrapper<DtrdExtEmsCategory> query = Wrappers.lambdaQuery(DtrdExtEmsCategory.class).eq(DtrdExtEmsCategory::getParentId, parentId)
                .eq(DtrdExtEmsCategory::getIsDel, 0).eq(DtrdExtEmsCategory::getDisplay, 1).orderByDesc(DtrdExtEmsCategory::getClassId);
        List<DtrdExtEmsCategory> list = baseMapper.selectList(query);
        if (list != null) {
            List<ArticleCategory> dataList = new ArrayList<>(list.size());
            list.stream().map(po -> new ArticleCategory().parseFromPO(po)).forEach(dataList::add);
            return dataList;
        }
        return null;
    }

    @Override
    public boolean addOrEditArticleCategory(EditArticleCategoryRequest request) {
        DtrdExtEmsCategory category = new DtrdExtEmsCategory();
        if (request.getClassId() == null) {
            category.init();
        }
        category.parseFromCategoryRequest(request);
        return saveOrUpdate(category);
    }

    @Override
    public void deleteArticleCategory(Integer classId) {
        // 讲分类下关联文章清空关联关系
        contentService.clearArticleClass(classId);
        // 删除分类
        baseMapper.deleteCategoryById(classId);
    }

    @Override
    public List<ArticleCategory> selectCategoryAndArticleCount(Integer parentId) {
        return baseMapper.selectCategoryAndArticleCount(parentId);
    }

    @Override
    public List<ArticleCategory> getAllCategory(Integer classId) {
        List<ArticleCategory> baseList = baseMapper.getAllCategory(classId);
        if (CollectionUtil.isNotEmpty(baseList)) {
            List<ArticleCategory> list = new ArrayList<>(baseList.size() / 2);
            Map<Integer, List<ArticleCategory>> tmpMap = new HashMap(baseList.size() / 2);
            baseList.forEach(category -> {
                // 处理一级分类
                if (category.getParentId() == 0) {
                    tmpMap.put(category.getClassId(), new ArrayList<>(5));
                    list.add(category);
                } else {
                    // 处理子分类
                    List<ArticleCategory> tmpList = tmpMap.get(category.getParentId());
                    tmpList.add(category);
                }
            });
            list.forEach(category -> category.setChildren(tmpMap.get(category.getClassId())));
            return list;
        }
        return Collections.emptyList();
    }

    @Override
    public String getCategoryNameById(Integer classId) {
        DtrdExtEmsCategory byId = getById(classId);
        return byId == null ? "" : byId.getName();
    }


}
